<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Menu as MenuIcon, User, House, Delete, Money, Histogram } from '@element-plus/icons-vue'

// 响应式变量
const sidebarOpened = ref(true)

// 路由相关
const router = useRouter()

// 切换侧边栏
const toggleSidebar = () => {
  sidebarOpened.value = !sidebarOpened.value;
  console.log('sidebarOpened.value', sidebarOpened.value);
}

// 处理菜单选择
const handleMenuSelect = (index: string) => {
  router.push({ name: index })
}
</script>

<template>
  <div class="app-container">
    <header class="app-header">
      <div class="header-left">
        <el-icon :size="24" @click="toggleSidebar"><MenuIcon /></el-icon>
        <span class="app-title">租客管理系统</span>
      </div>
      <div class="header-right">
        <el-dropdown placement="bottom-end">
          <span class="user-info">
            <el-icon :size="18"><User /></el-icon>
            <span>管理员</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <div class="app-content">
      <aside class="app-aside" :style="{ width: sidebarOpened ? '200px' : '64px' }">
        <transition name="slide" mode="out-in">
          <el-menu
            class="el-menu-vertical-demo"
            @select="handleMenuSelect"
            :collapse="!sidebarOpened"
            :collapse-transition="true"
          >
            <el-menu-item index="tenant">
              <el-icon><User /></el-icon>
              <span v-show="sidebarOpened">租客管理</span>
            </el-menu-item>
            <el-menu-item index="room">
              <el-icon><House /></el-icon>
              <span v-show="sidebarOpened">房间管理</span>
            </el-menu-item>
            <el-menu-item index="cleaning">
              <el-icon><Delete /></el-icon>
              <span v-show="sidebarOpened">卫生管理</span>
            </el-menu-item>
            <el-menu-item index="price">
              <el-icon><Money /></el-icon>
              <span v-show="sidebarOpened">房价管理</span>
            </el-menu-item>
            <el-menu-item index="report">
              <el-icon><Histogram /></el-icon>
              <span v-show="sidebarOpened">报表中心</span>
            </el-menu-item>
          </el-menu>
        </transition>
      </aside>

      <main class="app-main">
        <router-view />
      </main>
    </div>
  </div>
</template>

<style scoped>
.app-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-title {
  font-size: 18px;
  font-weight: bold;
  color: #1890ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.app-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.app-aside {
  color: #fff;
  transition: width 0.3s;
  overflow: hidden;
  height: calc(100vh - 60px);
}

.app-main {
  flex: 1;
  /* padding: 20px; */
  overflow-y: auto;
  background-color: #f5f5f5;
}

/* 添加菜单项的样式以改善视觉效果 */
:deep(.el-menu) {
  border-right: none;
}

:deep(.el-menu-item) {
  transition: all 0.3s;
}

:deep(.el-menu-item:hover) {
  background-color: #1890ff !important;
  color: #fff;
}

:deep(.el-menu-item.is-active) {
  background-color: #1890ff !important;
  color: #fff;
}
</style>
